package com.xb.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xb.entity.Announcement;
import com.xb.mapper.AnnouncementMapper;
import com.xb.service.IAnnouncementService;
import com.xb.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Service业务层处理
 */
@Service
public class AnnouncementServiceImp implements IAnnouncementService {
    @Resource
    private AnnouncementMapper announcementMapper;


    /**
     * 分页查询列表
     *
     * @param announcement
     * @return
     */
    @Override
    public Result selectAnnouncementList(Announcement announcement, Integer page, Integer limit) {
        Result result = new Result();
        PageHelper.startPage(page, limit);
        List<Announcement> list = announcementMapper.selectAnnouncementList(announcement);
        PageInfo<Announcement> announcementPageInfo = new PageInfo<>(list);
        if (announcementPageInfo.getList() != null) {
            result.setMessage("查询成功！");
            result.setData(announcementPageInfo.getList());
            result.setTotal(announcementPageInfo.getTotal());
            result.setStatus(Result.RESPONSE_SUCCESS);
        }
        return result;
    }

    /**
     * 查询列表
     *
     * @param announcement
     * @return
     */
    @Override
    public List<Announcement> selectAnnouncementList(Announcement announcement) {
        return announcementMapper.selectAnnouncementList(announcement);
    }

    /**
     * 新增
     *
     * @param announcement
     * @return 结果
     */
    @Override
    public Result insertAnnouncement(Announcement announcement) {
        Result result = new Result();
        if (announcementMapper.insertAnnouncement(announcement) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("添加成功！");
        } else {
            result.setMessage("添加失败！");
        }
        return result;
    }

    /**
     * 修改
     *
     * @param announcement
     * @return 结果
     */
    @Override
    public Result updateAnnouncement(Announcement announcement) {
        Result result = new Result();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        announcement.setAnnDate(sdf.format(date));
        if (announcementMapper.updateAnnouncement(announcement) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("修改成功！");
        } else {
            result.setMessage("修改失败！");
        }
        return result;
    }

    /**
     * 删除信息
     *
     * @param id ID
     * @return 结果
     */
    @Override
    public Result deleteAnnouncementById(Integer id) {


        Result result = new Result();
        if (announcementMapper.deleteAnnouncementById(id) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("删除成功！");
        } else {
            result.setMessage("删除失败！");
        }
        return result;
    }

    @Override
    public List<Announcement> selectAnnouncementListByStatus(int status) {
        return announcementMapper.selectAnnouncementListByStatus(status);
    }
}
